package com.shujia.base;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.ListPosition;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class RedisAPI {
    public static void main(String[] args) {
        //创建与redis数据库的连接对象
        //public Jedis(String host, int port)
        Jedis jedis = new Jedis("192.168.136.100", 7000);

        System.out.println("redis成功连接：" + jedis);

        System.out.println("===================五大基本数据类型操作========================");
        // string
//        System.out.println(jedis.set("name", "魏一民"));
//        System.out.println(jedis.set("name", "陈真"));
//        jedis.set("age","18");
//        jedis.set("address","安徽合肥");
//        jedis.set("like","二次元");

//        System.out.println(jedis.get("like"));
//        System.out.println(jedis.get("like2")); // 若键不存在返回null
//        List<String> mget = jedis.mget("name", "age", "address", "like");
//        for (String s : mget) {
//            System.out.println(s);
//        }

//        jedis.set("yzm","668766");
//        jedis.expire("yzm",10);

//        jedis.setex("yzm",10,"576234");


        // list
//        jedis.lpush("list1","hello","world","java","hadoop","hello");
//        List<String> list1 = jedis.lrange("list1", 0, -1);
//        System.out.println(list1);

        //long linsert(String var1, ListPosition var2, String var3, String var4);
//        jedis.linsert("list1", ListPosition.AFTER, "java","spark");

        // set
//        jedis.sadd("set1","hello","world","java","hello","hadoop","hello","java");
//        Set<String> set1 = jedis.smembers("set1");
//        System.out.println(set1);


        // zset 可排序的set
        //long zadd(String var1, Map<String, Double> var2);
        //先将元素和分数封装成一个Map集合
//        HashMap<String, Double> map1 = new HashMap<>();
//        map1.put("魏一民",78.0);
//        map1.put("陈真",69.0);
//        map1.put("李建国",81.0);
//        map1.put("徐思远",78.0);
//        map1.put("刘振田",100.0);
//        map1.put("周继豪",78.0);
//        jedis.zadd("scores",map1);

//        List<String> list2 = jedis.zrange("scores", 0, 110);
//        System.out.println(list2);
        //List<Tuple> zrangeWithScores(String var1, long var2, long var4);
//        List<Tuple> scores = jedis.zrangeWithScores("scores", 0, 110);
//        for (Tuple score : scores) {
//            String name = score.getElement();
//            double s = score.getScore();
//            System.out.println(name+": "+s);
//        }
//        List<Tuple> scores = jedis.zrevrangeWithScores("scores", 0, 110);
//        for (Tuple score : scores) {
//            String name = score.getElement();
//            double s = score.getScore();
//            System.out.println(name + ": " + s);
//        }


        // hash
        //将要添加的键值元素封装成一个map集合
        HashMap<String, String> map = new HashMap<>();
        map.put("name","小虎");
        map.put("age","18");
        map.put("content","安徽数加");
        map.put("address","安徽合肥");

        //long hset(String var1, Map<String, String> var2);
        jedis.hset("1001",map);


        //释放资源
        jedis.close();
    }
}
